package com.xiaowu.netmusic.controller;

import com.xiaowu.netmusic.service.HistoryService;
import com.xiaowu.netmusic.utils.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/history")
@AllArgsConstructor
@Tag(name = "历史记录管理", description = "提供用户播放历史记录相关操作接口")
public class HistoryController {

    private final HistoryService historyService;

    @Operation(summary = "记录播放历史", description = "记录用户播放的歌曲历史")
    @PostMapping("/record")
    public Result<?> record(@RequestParam Long songId, @RequestHeader("userId") Long userId) {
        historyService.recordPlay(userId, songId);
        return Result.success();
    }

    @Operation(summary = "获取历史记录", description = "获取用户的播放历史记录列表")
    @GetMapping("/list")
    public Result<?> list(@RequestHeader("userId") Long userId,
                          @RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "10") int size) {
        return Result.success(historyService.getUserHistory(userId, page, size));
    }

    @Operation(summary = "清空历史记录", description = "清空用户的所有播放历史记录")
    @DeleteMapping("/clear")
    public Result<?> clearHistory(@RequestHeader("userId") Long userId) {
        historyService.clearUserHistory(userId);
        return Result.success("历史记录已清空");
    }

    @Operation(summary = "删除历史记录", description = "删除用户的某条播放历史记录")
    @DeleteMapping("/delete/{historyId}")
    public Result<?> deleteHistory(@PathVariable Long historyId, @RequestHeader("userId") Long userId) {
        historyService.deleteHistory(historyId, userId);
        return Result.success("历史记录已删除");
    }
}
